/* Copyright Statement:
*
* This software/firmware and related documentation ("MediaTek Software") are
* protected under relevant copyright laws. The information contained herein
* is confidential and proprietary to MediaTek Inc. and/or its licensors.
* Without the prior written permission of MediaTek inc. and/or its licensors,
* any reproduction, modification, use or disclosure of MediaTek Software,
* and information contained herein, in whole or in part, shall be strictly prohibited.
*/
/* MediaTek Inc. (C) 2015. All rights reserved.
*
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
* THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
* CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
* SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
* CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* The following software/firmware and/or related documentation ("MediaTek Software")
* have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
* applicable license agreements with MediaTek Inc.
*/
#ifndef BUILD_LK
#include <linux/string.h>
#endif
#include "lcm_drv.h"

#ifdef BUILD_LK
#include <platform/mt_gpio.h>
#include <platform/mt_i2c.h>
#ifdef MTK_MT6360_LDO_SUPPORT
#include <mt6360_ldo.h>
#endif
#elif defined(BUILD_UBOOT)
#include <asm/arch/mt_gpio.h>
#else
//	#include <mach/mt_pm_ldo.h>
//  #include <mach/mt_gpio.h>
#endif

#ifdef BUILD_LK
#define LCD_DEBUG(fmt)  dprintf(CRITICAL,fmt)
#else
#define LCD_DEBUG(fmt)  printk(fmt)
#endif

static LCM_UTIL_FUNCS lcm_util;

#define SET_RESET_PIN(v) (lcm_util.set_reset_pin((v)))
#define MDELAY(n) (lcm_util.mdelay(n))

/* ----------------------------------------------------------------- */
/*  Local Functions */
/* ----------------------------------------------------------------- */
#define dsi_set_cmdq_V2(cmd, count, ppara, force_update) \
	lcm_util.dsi_set_cmdq_V2(cmd, count, ppara, force_update)
#define dsi_set_cmdq(pdata, queue_size, force_update) \
	lcm_util.dsi_set_cmdq(pdata, queue_size, force_update)
#define wrtie_cmd(cmd) lcm_util.dsi_write_cmd(cmd)
#define write_regs(addr, pdata, byte_nums) \
	lcm_util.dsi_write_regs(addr, pdata, byte_nums)
#define read_reg(cmd) lcm_util.dsi_dcs_read_lcm_reg(cmd)
#define read_reg_v2(cmd, buffer, buffer_size) \
	lcm_util.dsi_dcs_read_lcm_reg_v2(cmd, buffer, buffer_size)

static const unsigned char LCD_MODULE_ID = 0x01;

/* ----------------------------------------------------------------- */
/*  Local Constants */
/* ----------------------------------------------------------------- */
#define LCM_DSI_CMD_MODE           0
#define FRAME_WIDTH                (1200)
#define FRAME_HEIGHT               (1920)
#define GPIO_OUT_ONE               1
#define GPIO_OUT_ZERO              0
#define REGFLAG_DELAY              0xFE
#define REGFLAG_END_OF_TABLE       0xFF

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifdef GPIO_LCD_BIAS_ENP_PIN
#define GPIO_LCD_PWR_ENP           GPIO_LCD_BIAS_ENP_PIN
#else
#define GPIO_LCD_PWR_ENP           GPIO23
#endif

#ifdef GPIO_LCM_RST
#define GPIO_LCD_RST               GPIO_LCM_RST
#else
#define GPIO_LCD_RST               GPIO44
#endif

#ifdef GPIO_CTP_RST_PIN
#define GPIO_CTP_RST               GPIO_CTP_RST_PIN
#else
#define GPIO_CTP_RST               GPIO125
#endif

#ifdef GPIO_LCD_BIAS_ENN_PIN
#define GPIO_LCD_PWR_ENN           GPIO_LCD_BIAS_ENN_PIN
#else
#define GPIO_LCD_PWR_ENN           GPIO202
#endif

/* ----------------------------------------------------------------- */
/*  Local Variables */
/* ----------------------------------------------------------------- */
static void lcm_set_gpio_output(unsigned int GPIO, unsigned int output)
{
	if (GPIO == 0xFFFFFFFF) {
		printf("[LK/LCM] GPIO not defined\n");
		return;
	}
	mt_set_gpio_mode(GPIO, GPIO_MODE_00);
	mt_set_gpio_dir(GPIO, GPIO_DIR_OUT);
	mt_set_gpio_out(GPIO, (output > 0) ? GPIO_OUT_ONE : GPIO_OUT_ZERO);
}

struct LCM_setting_table {
	unsigned char cmd;
	unsigned char count;
	unsigned char para_list[64];
};

/* Panel initial code is default in FT8201 flash.
 * So only need control power sequence if we needn't update initial.
 * No need update initial code: FTS_AUTO_LIC_UPGRADE_EN=0(default)
 * Need update initial code: FTS_AUTO_LIC_UPGRADE_EN=1(enable)
 * As backlight is from panel control, so initial code is needed.
*/

static struct LCM_setting_table lcm_init_setting[] = {
	//Stop reload
	{0x50, 1, {0x5A} },
	//INT CANCEL
	{0x54, 1, {0x03} },
	//VCOM
	{0x50, 2, {0x5A, 0x03} },
	{0x80, 1, {0xC2} },
	//Blank 8
	{0x50, 2, {0x5A, 0x08} },
	{0x80, 2, {0xC8, 0x00} },
	//Blank 9
	{0x50, 2, {0x5A, 0x09} },
	{0x80, 16, {0x5A, 0x51, 0xB5, 0xAA, 0x6A, 0xD5, 0x4A, 0x01,
		0x00, 0xE1, 0x0D, 0x82, 0x20, 0x08, 0x00, 0xB4} },
	{0x90, 16, {0x00, 0x24, 0x42, 0x0A, 0xE3, 0x6D, 0x1B, 0xF1,
		0xB9, 0xDC, 0x6B, 0x20, 0x69, 0xA1, 0x26, 0x00} },
	{0xA0, 6, {0xA1, 0x80, 0x08, 0x20, 0x06, 0x00} },
	//Blank 10
	{0x50, 2, {0x5A, 0x0A} },
	{0x80, 16, {0x62, 0xC5, 0x02, 0x09, 0x17, 0x23, 0x32, 0x43,
		0x55, 0x5B, 0x8E, 0x6C, 0xA1, 0x9C, 0x6E, 0x95} },
	{0x90, 16, {0x66, 0x60, 0x4E, 0x3C, 0x29, 0x17, 0x08, 0x09,
		0x17, 0x23, 0x32, 0x43, 0x55, 0x5B, 0x8E, 0x6C} },
	{0xA0, 12, {0xA1, 0x9C, 0x6E, 0x95, 0x66, 0x60, 0x4E, 0x3C,
		0x29, 0x17, 0x08, 0x00} },
	//Blank 11
	{0x50, 2, {0x5A, 0x0B} },
	{0x80, 16, {0x99, 0x99, 0x29, 0x44, 0x08, 0x00, 0x60, 0x47,
		0x00, 0x00, 0x10, 0x22, 0x04, 0x00, 0xB0, 0x03} },
	{0x90, 2, {0x15, 0x00} },
	//Blank 12
	{0x50, 2, {0x5A, 0x0C} },
	{0x80, 16, {0xFD, 0x68, 0x58, 0x02, 0x3C, 0x64, 0xB8, 0x06,
		0x00, 0x60, 0x15, 0x00, 0x50, 0x15, 0x56, 0x51} },
	{0x90, 16, {0x15, 0x55, 0x61, 0x15, 0x00, 0x60, 0x15, 0x00,
		0x50, 0x15, 0x56, 0x51, 0x15, 0x55, 0x61, 0x95} },
	{0xA0, 16, {0xAB, 0x18, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
		0x80, 0x4C, 0x29, 0x84, 0x52, 0x01, 0x09, 0x00} },
	//Blank 13
	{0x50, 2, {0x5A, 0x0D} },
	{0x80, 14, {0xF0, 0xB1, 0x71, 0xEF, 0x49, 0xC0, 0x80, 0x1A,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 14
	{0x50, 2, {0x5A, 0x0E} },
	{0x80, 12, {0xFF, 0x81, 0x68, 0xAC, 0x22, 0x6D, 0x24, 0x00,
		0x00, 0x00, 0x00, 0x00} },
	//Blank 15
	{0x50, 2, {0x5A, 0x0F} },
	{0x80, 16, {0x0D, 0x08, 0x4A, 0x00, 0x25, 0x80, 0x64, 0x0C,
		0x51, 0x21, 0xC8, 0x50, 0x03, 0x4A, 0x8C, 0x8C} },
	{0x90, 7, {0x24, 0x69, 0x81, 0x71, 0x44, 0x7C, 0x14} },
	//Blank 16
	{0x50, 2, {0x5A, 0x10} },
	{0x80, 16, {0x00, 0x00, 0x03, 0xE7, 0x1F, 0x17, 0x00, 0x78,
		0x00, 0x03, 0xD4, 0x18, 0x30, 0x88, 0x41, 0x8A} },
	{0x90, 15, {0x39, 0x28, 0xA9, 0xC5, 0x9A, 0x7B, 0xF5, 0x07,
		0x7E, 0xE0, 0x07, 0x7E, 0x20, 0x10, 0x00} },
	//Blank 17
	{0x50, 2, {0x5A, 0x11} },
	{0x80, 16, {0x43, 0x77, 0x03, 0x40, 0x8A, 0xF1, 0xFF, 0x43,
		0x30, 0x08, 0xC1, 0x46, 0xA0, 0x80, 0x24, 0x18} },
	{0x90, 16, {0x30, 0x06, 0x64, 0xC1, 0x80, 0x21, 0x07, 0xC4,
		0xE1, 0xD0, 0x68, 0x40, 0x20, 0x1D, 0x04, 0x48} },
	{0xA0, 16, {0x40, 0x40, 0x28, 0x10, 0x90, 0x70, 0x38, 0x1A,
		0x10, 0x47, 0x03, 0xE2, 0x20, 0x00, 0x1C, 0x80} },
	{0xB0, 16, {0x50, 0x30, 0x00, 0xE0, 0xE1, 0x01, 0x00, 0x28,
		0x0E, 0x06, 0xC3, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC} },
	{0xC0, 16, {0x8C, 0x88, 0x88, 0x88, 0x88, 0x88, 0xC8, 0x08,
		0x86, 0xC6, 0xE3, 0x81, 0x00, 0x20, 0x00, 0x21} },
	{0xD0, 16, {0x42, 0x88, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
		0x00, 0x31, 0x04, 0x41, 0x06, 0x00, 0x00, 0x00} },
	{0xE0, 16, {0x00, 0x92, 0x24, 0x49, 0x92, 0x04, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x85, 0x11, 0x0C} },
	{0xF0, 9, {0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x5E,
		0x00} },
	//Blank 18
	{0x50, 2, {0x5A, 0x12} },
	{0x80, 16, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 9, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00} },
	//Blank 19
	{0x50, 2, {0x5A, 0x13} },
	{0x80, 16, {0x01, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00,
		0x01, 0x02, 0x01, 0x01, 0x44, 0x0C, 0xCE, 0xE7} },
	{0x90, 3, {0x62, 0x06, 0x00} },
	//Blank 20
	{0x50, 2, {0x5A, 0x14} },
	{0x80, 16, {0x51, 0x01, 0x40, 0x5E, 0xE9, 0xEF, 0xF7, 0xFB,
		0xFD, 0x7E, 0x00, 0x00, 0x00, 0xE0, 0xC5, 0x86} },
	{0x90, 16, {0x1B, 0x2C, 0x20, 0x2E, 0xDC, 0x60, 0x01, 0x31,
		0xE1, 0x06, 0x0B, 0x88, 0x07, 0x37, 0x58, 0x80} },
	{0xA0, 16, {0x39, 0xB0, 0xC1, 0x08, 0xCC, 0x80, 0x0D, 0x46,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0xB0, 16, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x24, 0x3C, 0xC2, 0x2B, 0x00, 0x40, 0xA1} },
	{0xC0, 16, {0x50, 0x78, 0x03, 0x6E, 0xB0, 0x80, 0x31, 0x00,
		0x2F, 0xC0, 0x0D, 0x16, 0x30, 0x06, 0xE0, 0x15} },
	{0xD0, 16, {0xB8, 0xC1, 0x02, 0xC6, 0x00, 0xBC, 0x04, 0x37,
		0x58, 0xC0, 0x18, 0x80, 0x37, 0xE1, 0x06, 0x0B} },
	{0xE0, 16, {0x18, 0x03, 0xF0, 0x1E, 0xDC, 0x60, 0x01, 0x63,
		0x00, 0xDE, 0x82, 0x1B, 0x2C, 0x60, 0x0C, 0xC0} },
	{0xF0, 16, {0x3B, 0x70, 0x83, 0x05, 0x8C, 0x01, 0x00, 0x01,
		0xBF, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 21
	{0x50, 2, {0x5A, 0x15} },
	{0x80, 16, {0x00, 0x68, 0x62, 0xE0, 0x06, 0x03, 0x03, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 16, {0x00, 0x00, 0xC7, 0x6B, 0x83, 0x38, 0xBE, 0x86,
		0xD0, 0x6A, 0x1F, 0x31, 0x6A, 0xB5, 0x21, 0xB4} },
	{0xA0, 16, {0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0xB0, 16, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x13, 0x00, 0xF0, 0x00, 0x14, 0x82, 0x00, 0x00} },
	{0xC0, 16, {0x08, 0xA2, 0xA8, 0x75, 0x5D, 0x57, 0x04, 0x61,
		0x18, 0x55, 0x55, 0xD5, 0x75, 0x55, 0x14, 0xC7} },
	{0xD0, 16, {0xB1, 0x2C, 0x49, 0x52, 0xD5, 0x75, 0x35, 0x0C,
		0x41, 0x50, 0xD5, 0x75, 0x5D, 0x57, 0xC3, 0x10} },
	{0xE0, 16, {0x04, 0x49, 0xB2, 0xAC, 0x75, 0x5D, 0x57, 0x45,
		0x71, 0x1C, 0x55, 0x55, 0xD5, 0x75, 0x45, 0x10} },
	{0xF0, 16, {0x86, 0xA1, 0x28, 0x08, 0x52, 0xD5, 0x75, 0x25,
		0x08, 0x00, 0x50, 0xD5, 0x75, 0x5D, 0x57, 0x02} },
	//Blank 22
	{0x50, 2, {0x5A, 0x16} },
	{0x80, 16, {0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 6, {0x00, 0x00, 0x00, 0x00, 0xC0, 0x40} },
	//Blank 24
	{0x50, 2, {0x5A, 0x18} },
	{0x80, 16, {0xEF, 0xBD, 0xF7, 0xDE, 0x7B, 0xEF, 0xBD, 0x07,
		0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08} },
	{0x90, 16, {0xA8, 0x09, 0xA8, 0x99, 0x08, 0x08, 0x08, 0x08,
		0x08, 0x08, 0x08, 0x08, 0x18, 0x09, 0x18, 0x99} },
	{0xA0, 16, {0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
		0x98, 0x08, 0x98, 0x08, 0x04, 0xFF, 0x05, 0x80} },
	{0xB0, 15, {0x80, 0x00, 0x04, 0x20, 0x00, 0x01, 0x08, 0x40,
		0x00, 0x02, 0x10, 0x80, 0x00, 0x04, 0x0F} },
	//Blank 25
	{0x50, 2, {0x5A, 0x19} },
	{0x80, 16, {0x95, 0x93, 0x8F, 0x8C, 0x80, 0x8B, 0x89, 0x87,
		0x86, 0x84, 0x83, 0x83, 0x00, 0x60, 0xF6, 0xFF} },
	{0x90, 16, {0xFF, 0x2F, 0xF3, 0xEF, 0xFF, 0xBF, 0x0F, 0x0F,
		0xAF, 0xB5, 0x71, 0x0E, 0x6C, 0x4A, 0x69, 0x08} },
	{0xA0, 7, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 26
	{0x50, 2, {0x5A, 0x1A} },
	{0x80, 16, {0x00, 0x04, 0x08, 0x0C, 0x00, 0x10, 0x14, 0x18,
		0x1C, 0x00, 0x20, 0x28, 0x30, 0x38, 0x00, 0x40} },
	{0x90, 16, {0x48, 0x50, 0x58, 0x00, 0x60, 0x68, 0x70, 0x78,
		0x00, 0x80, 0x88, 0x90, 0x98, 0x00, 0xA0, 0xA8} },
	{0xA0, 16, {0xB0, 0xB8, 0x00, 0xC0, 0xC8, 0xD0, 0xD8, 0x00,
		0xE0, 0xE8, 0xF0, 0xF8, 0x00, 0xFC, 0xFE, 0xFF} },
	{0xB0, 16, {0x00, 0x00, 0x04, 0x08, 0x0C, 0x00, 0x10, 0x14,
		0x18, 0x1C, 0x00, 0x20, 0x28, 0x30, 0x38, 0x00} },
	{0xC0, 16, {0x40, 0x48, 0x50, 0x58, 0x00, 0x60, 0x68, 0x70,
		0x78, 0x00, 0x80, 0x88, 0x90, 0x98, 0x00, 0xA0} },
	{0xD0, 16, {0xA8, 0xB0, 0xB8, 0x00, 0xC0, 0xC8, 0xD0, 0xD8,
		0x00, 0xE0, 0xE8, 0xF0, 0xF8, 0x00, 0xFC, 0xFE} },
	{0xE0, 16, {0xFF, 0x00, 0x00, 0x04, 0x08, 0x0C, 0x00, 0x10,
		0x14, 0x18, 0x1C, 0x00, 0x20, 0x28, 0x30, 0x38} },
	{0xF0, 16, {0x00, 0x40, 0x48, 0x50, 0x58, 0x00, 0x60, 0x68,
		0x70, 0x78, 0x00, 0x80, 0x88, 0x90, 0x98, 0x00} },
	//Blank 27
	{0x50, 2, {0x5A, 0x1B} },
	{0x80, 16, {0xA0, 0xA8, 0xB0, 0xB8, 0x00, 0xC0, 0xC8, 0xD0,
		0xD8, 0x00, 0xE0, 0xE8, 0xF0, 0xF8, 0x00, 0xFC} },
	{0x90, 4, {0xFE, 0xFF, 0x00, 0x00} },
	//Blank 32
	{0x50, 2, {0x5A, 0x20} },
	{0x80, 7, {0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	//Blank 34
	{0x50, 2, {0x5A, 0x22} },
	{0x80, 13, {0x2D, 0xD3, 0x00, 0x41, 0x10, 0x04, 0x41, 0x10,
		0x04, 0x41, 0x10, 0x1E, 0x00} },
	//Blank 35
	{0x50, 2, {0x5A, 0x23} },
	{0x80, 16, {0x01, 0x05, 0x00, 0x05, 0x00, 0x00, 0x01, 0x00,
		0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
	{0x90, 9, {0xFF, 0x0F, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00,
		0x00} },
	//Blank 36
	{0x50, 2, {0x5A, 0x24} },
	{0x80, 16, {0x00, 0x03, 0x00, 0x9D, 0xBF, 0x10, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF9, 0x7B} },
	//CABC control
	//{0x50, 2, {0x5A, 0x23} },
	//{0x95, 1, {0x01} },
	//SPI not FINISH
	//SPI not FINISH
	//{0x90, 1, {0x5A} },
	//SPI FINISH
	{0x90, 1, {0xA5} },
	//Blank select 2F
	{0x50, 2, {0x5A, 0x2F} },
	{0x09, 1, {0x1} },
	{0x11, 0, { } },
	{REGFLAG_DELAY, 120, {} },
	{0x29, 0, { } },
};

static void push_table(struct LCM_setting_table *table,
	unsigned int count, unsigned char force_update)
{
	unsigned int i;

	for (i = 0; i < count; i++) {
		unsigned int cmd;

		cmd = table[i].cmd;
		switch (cmd) {
		case REGFLAG_DELAY:
			MDELAY(table[i].count);
			break;

		case REGFLAG_END_OF_TABLE:
			break;

		default:
			dsi_set_cmdq_V2(cmd,
				table[i].count,
				table[i].para_list,
				force_update);
		}
	}
}

static void lcm_set_util_funcs(const LCM_UTIL_FUNCS *util)
{
	memcpy(&lcm_util, util, sizeof(LCM_UTIL_FUNCS));
}

static void lcm_get_params(LCM_PARAMS *params)
{
	memset(params, 0, sizeof(LCM_PARAMS));

	params->type   = LCM_TYPE_DSI;

	params->width  = FRAME_WIDTH;
	params->height = FRAME_HEIGHT;

#if (LCM_DSI_CMD_MODE)
	params->dsi.mode   = CMD_MODE;
#else
	params->dsi.mode   = SYNC_EVENT_VDO_MODE;
#endif

	params->dsi.LANE_NUM                = LCM_FOUR_LANE;

	params->dsi.data_format.color_order = LCM_COLOR_ORDER_RGB;
	params->dsi.data_format.trans_seq   = LCM_DSI_TRANS_SEQ_MSB_FIRST;
	params->dsi.data_format.padding     = LCM_DSI_PADDING_ON_LSB;
	params->dsi.data_format.format      = LCM_DSI_FORMAT_RGB888;

	params->dsi.packet_size             = 256;
	params->dsi.PS                      = LCM_PACKED_PS_24BIT_RGB888;

	params->dsi.vertical_sync_active    = 1;
	params->dsi.vertical_backporch      = 32;
	params->dsi.vertical_frontporch     = 155;
	params->dsi.vertical_active_line    = FRAME_HEIGHT;

	params->dsi.horizontal_sync_active  = 1;
	params->dsi.horizontal_backporch    = 12;
	params->dsi.horizontal_frontporch   = 55;
	params->dsi.horizontal_active_pixel = FRAME_WIDTH;
	//params->dsi.ssc_disable                      = 1;
#if (LCM_DSI_CMD_MODE)
	params->dsi.PLL_CLOCK = 474;
#else
	params->dsi.PLL_CLOCK = 474;
#endif
}

#define RT4801_SLAVE_ADDR_WRITE  0xE6
#define I2C_RT4801_PMU_CHANNEL 0
#define I2C_RT4801_PMU_SLAVE_7_BIT_ADDR 0x73

static int RT4801_read_byte (kal_uint8 addr, kal_uint8 *dataBuffer)
{
    kal_uint32 ret = I2C_OK;
    kal_uint16 len;
    struct mt_i2c_t RT4801_i2c;
    *dataBuffer = addr;

    RT4801_i2c.id = I2C_RT4801_PMU_CHANNEL;
    RT4801_i2c.addr = I2C_RT4801_PMU_SLAVE_7_BIT_ADDR;
    RT4801_i2c.mode = ST_MODE;
    RT4801_i2c.speed = 400;
    len = 1;

    ret = i2c_write_read(&RT4801_i2c, dataBuffer, len, len);
    if (I2C_OK != ret)
	printf("%s: i2c_read  failed! ret: %d\n", __func__, ret);

    return ret;
}

static int RT4801_write_byte(kal_uint8 addr, kal_uint8 value)
{
    kal_uint32 ret_code = I2C_OK;
    kal_uint8 write_data[2];
    kal_uint16 len;
    struct mt_i2c_t RT4801_i2c;

    write_data[0] = addr;
    write_data[1] = value;

    RT4801_i2c.id = I2C_RT4801_PMU_CHANNEL;
    RT4801_i2c.addr = I2C_RT4801_PMU_SLAVE_7_BIT_ADDR;
    RT4801_i2c.mode = ST_MODE;
    RT4801_i2c.speed = 400;
    len = 2;

    ret_code = i2c_write(&RT4801_i2c, write_data, len);

    return ret_code;
}

static int RT4801_REG_MASK (kal_uint8 addr, kal_uint8 val, kal_uint8 mask)
{
    kal_uint8 RT4801_reg = 0;
    kal_uint32 ret = 0;

    ret = RT4801_read_byte(addr, &RT4801_reg);

    RT4801_reg &= ~mask;
    RT4801_reg |= val;

    ret = RT4801_write_byte(addr, RT4801_reg);

    return ret;
}

static void lcm_init(void)
{
	int ret = 0;

	printf("[LK/LCM] %s\n", __func__);

#ifdef MTK_MT6360_LDO_SUPPORT
	mt6360_ldo_enable(MT6360_LDO2, true);
	MDELAY(5);
#endif

	lcm_set_gpio_output(GPIO_LCD_PWR_ENP, GPIO_OUT_ONE);
	lcm_set_gpio_output(GPIO_LCD_PWR_ENN, GPIO_OUT_ONE);
	MDELAY(100);
	/* set AVDD = 5.8V */
	ret = RT4801_REG_MASK(0x00, 0x12, (0x1F << 0));
	if (ret < 0)
		printf("[LK/LCM] cmd=%0x, i2c write error\n", 0x00);
	else
		printf("[LK/LCM] cmd=%0x, i2c write success\n", 0x00);
	/* set AVEE = -5.8V */
	ret = RT4801_REG_MASK(0x01, 0x12, (0x1F << 0));
	if (ret < 0)
		printf("[LK/LCM] cmd=%0x, i2c write error\n", 0x01);
	else
		printf("[LK/LCM] cmd=%0x, i2c write success\n", 0x01);

	/* enable AVDD & AVEE discharge*/
	ret = RT4801_REG_MASK(0x03, (1<<0) | (1<<1), (1<<0) | (1<<1));
	if (ret < 0)
		printf("[LK/LCM] cmd=%0x, i2c write error\n", 0x03);
	else
		printf("[LK/LCM] cmd=%0x, i2c write success\n", 0x03);
	MDELAY(8);

	lcm_set_gpio_output(GPIO_CTP_RST, GPIO_OUT_ONE);
	MDELAY(10);

	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ONE);
	MDELAY(10);
	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ZERO);
	MDELAY(10);
	lcm_set_gpio_output(GPIO_LCD_RST, GPIO_OUT_ONE);
	MDELAY(150);

	push_table(lcm_init_setting,
		sizeof(lcm_init_setting) / sizeof(struct LCM_setting_table),
		1);
}

LCM_DRIVER ft8201_wuxga_dsi_vdo_boe_lcm_drv= {
	.name               = "ft8201_wuxga_dsi_vdo_boe",
	.set_util_funcs     = lcm_set_util_funcs,
	.get_params         = lcm_get_params,
	.init               = lcm_init,
};

